/*
 * @Description: 扫描界面
 * @Version: V1.0.0
 * @Author: 黄鑫
 * @Date: 2024-12-11
 * @LastEditors: 黄鑫
 * @LastEditTime: 2025-05-12
 * @FilePath: ScanPage.dart
 * @Remarks: 
 */
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

class ScanPage extends StatefulWidget {
  Function(String qrString) callbackResult;
  ScanPage({super.key, required this.callbackResult});

  @override
  State<ScanPage> createState() => _ScanPageState();
}

class _ScanPageState extends State<ScanPage>
    with SingleTickerProviderStateMixin {
  MobileScannerController scannerController =
      MobileScannerController(returnImage: true);

  late AnimationController animationController;
  late Animation<double> _animation;

  var imageUrl = Rxn<Uint8List>();

  @override
  void initState() {
    super.initState();

    // 初始化动画控制器
    animationController = AnimationController(
      duration: const Duration(seconds: 3),
      vsync: this,
    )..repeat(); // 无限循环

    // 创建从 0 到 1 的动画
    _animation = Tween<double>(begin: 0, end: 1).animate(CurvedAnimation(
      parent: animationController,
      curve: Curves.linear,
    ));
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  // 处理二维码数据
  handleQrString(var qrString) async {
    scannerController.stop();
    widget.callbackResult(qrString);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("扫描"),
      ),
      backgroundColor: Colors.black,
      body: bodyView(),
    );
  }

  bodyView() {
    return Obx(() => Stack(
          children: [
            Visibility(
              visible: imageUrl.value != null,
              child: imageUrl.value != null
                  ? Image.memory(
                      imageUrl.value!,
                      width: double.infinity,
                      height: double.infinity,
                      fit: BoxFit.cover,
                    )
                  : Container(),
            ),
            Visibility(
              visible: imageUrl.value == null,
              child: MobileScanner(
                controller: scannerController,
                onDetect: (barcodes) {
                  List<Barcode> barcodeList = barcodes.barcodes;
                  Uint8List barcodeImage = barcodes.image!;
                  imageUrl.value = barcodeImage;
                  if (barcodeList.isNotEmpty) {
                    Barcode barcode = barcodeList.first;
                    if (barcode.rawValue != null) {
                      handleQrString(barcode.rawValue!);
                    }
                  }
                },
              ),
            ),
            Visibility(
              visible: imageUrl.value == null,
              child: AnimatedBuilder(
                animation: _animation,
                builder: (context, child) {
                  return Positioned(
                    top: _animation.value * 400,
                    left: 20,
                    right: 20,
                    child: Container(
                      height: 2,
                      color: Colors.green,
                    ),
                  );
                },
              ),
            )
          ],
        ));
  }
}
